var roleType=1;//员工0，广告主1
var roleName1 = "";


var page =1;//默认页数
var pageSize = 10;//每页条数
var total = 0;//总条数
//分页方法
function paging(page,pageSize,total){
	page = Number(page);
	
	//最大页数
	var maxPage = Math.ceil(total/pageSize) || 1;
	if(page>maxPage){
		//取值不能超过maxPage
	    page = Math.min(page,maxPage);
	    tiaozhuan(param,page,pageSize,roleType,roleName1);
	}else if(page<1){
		//取值不能小于1
	    page = Math.max(page,1);
	    tiaozhuan(param,page,pageSize,roleType,roleName1);
	}else{
		//	当前页数
	$('.ui-paging-count').val(page);
	//首页的置灰处理
	$('.js-page-first').removeClass('ui-pager-disabled');
	//上一页
	$('.js-page-prev').removeClass('ui-pager-disabled').attr("data-page",page-1);
	//下一页
	$('.js-page-next').removeClass('ui-pager-disabled').attr("data-page",page+1);
	//最末页
	$('.js-page-last').removeClass('ui-pager-disabled').attr("data-page",maxPage);
	//总页数显示
	$('.js-page-total').text(maxPage);
	
	
	//当前显示多少页的处理
	$(".ui-select-pagesize").val(pageSize);
	//第一页的处理
	if(page == 1){
		$(".js-page-first").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-prev").addClass('ui-pager-disabled').attr("data-page","");
	}
	if(page == maxPage){
		//最后一页的处理
		$(".js-page-last").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-next").addClass('ui-pager-disabled').attr("data-page","");
	}
	}
	 
	
};
//下拉选择分页条数的操作
$('.ui-select-pagesize').on("change",function(){
	pageSize = $(this).val();	
	tiaozhuan(param,page,pageSize,roleType,roleName1);
});

//跳转到X页  blur
$(".ui-paging-count").on("blur",function(){
	page = $(this).val();
	tiaozhuan(param,page,pageSize,roleType,roleName1);
});

//上一页，下一页，首页，最末页的跳转
$('.ui-pager').on("click",function(){
	page = $(this).attr("data-page");
	tiaozhuan(param,page,pageSize,roleType,roleName1);
});

//角色名查找
$('.clickFunc').on("click",function(){
	roleName1 = $('#searchRalename').val();
	tiaozhuan(param,page,pageSize,roleType,roleName1);
});

function tiaozhuan(param,page,pageSize,roleType,roleName){
	var html = "";
	method = "post";
	datas = {page:page,pageSize:pageSize,roleType:roleType,roleName:roleName};
	getAgentMsg(param,method,datas);
	for(var i=0;i<dataResult.data.rows.length;i++){
		html += "<tr>";
			html += "<td>"+dataResult.data.rows[i].roleName+"</td>";
			html += "<td>"+dataResult.data.rows[i].remark+"</td>";
			html += "<td><a class='dsp-btn btn-blue' href='javascript:void(0)' data-roleType='"+dataResult.data.rows[i].roleType+"' data-id='"+dataResult.data.rows[i].roleId+"' onclick='getRoleList(this)'>编辑</a>";
			html += "</td>";
		html += "</tr>";
	}
	$(".roleList").html(html);
	//之后处理分页
	total = dataResult.data.total;
	paging(page,pageSize,total);
}
//编辑角色按钮
function getRoleList(obj){
	var goUrl = "permissions.html";
	var roleId = $(obj).attr("data-id");
	var roleType = $(obj).attr("data-roleType");
	var remark = $(obj).parent().prev().text();
	var roleName = $(obj).parent().prev().prev().text();
	
	window.location.href = goUrl+"?token="+token+"&id="+token_id+"&roleId1="+roleId+"&remark1="+remark+"&roleName1="+roleName+"&roleType1="+roleType;
}

//添加员工角色
function goPermissions(){
	window.location.href = "permissions.html?token="+token+"&id="+token_id+"&roleType1=1";
}
function gorole2(){
	window.location.href = "role2.html?token="+token+"&id="+token_id;
}
//初始化加载数据
window.onload = function(){
	param = "/sys/roleList";
	tiaozhuan(param,page,pageSize,roleType,roleName1);
}